﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using University.Workshop.Business;
using System.Resources;
using University.Workshop.Model;

namespace University.Workshop.WinApp
{
    public partial class BookingSearchView :TemplateForm
    {
        String usedGrupBox;
        private ResourceManager resours;
        public BookingSearchView()
        {
            InitializeComponent();
            resours = new ResourceManager(typeof(BookingRoomForm));
        }

        private void SearchButton_Click(object sender, EventArgs e)
        {
            resultDataGridView.Rows.Clear();
            // busqueda segun el ultimo que ha cambiado
            String option=usedGrupBox;
            switch (option) {
                case "searchTypeGroupBox": {
                    //Searching by type of room
                    String roomType = (String)comboBoxTypes.Text.Trim();
                    int idType = RoomStuffManager.GetByName(roomType);
                    if (!BookingManager.SearchAllBookedRoomsByType(resultDataGridView, idType)){
                        MessageBox.Show("There is no Booked room for this Type", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }
                    break;
                }
                case "searchByDateGroupBox":{
                    //Searching by dates
                    // VAlidar las fechas
                    // Obtener Todos los cuartos que esten dentro del rango de la fecha
                    DateTime checkInDate = CheckIndateTimePicker.Value.Date;
                    DateTime checkOutDate = CheckOutdateTimePicker.Value.Date;
                    if (!BookingManager.SearchAllBookedRoomsByDate(resultDataGridView, checkInDate, checkOutDate)) {
                        MessageBox.Show("There is no Booked room for this Dates", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }
                    break;
                }
                default: {
                    //No searching
                    MessageBox.Show("Invalid Search", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    break;
                }
            }
        }

        private void comboBoxTypes_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.usedGrupBox = "searchTypeGroupBox";
        }

        private void CheckIndateTimePicker_ValueChanged(object sender, EventArgs e)
        {
            this.usedGrupBox = "searchByDateGroupBox";
        }

        private void CheckOutdateTimePicker_ValueChanged(object sender, EventArgs e)
        {
            this.usedGrupBox = "searchByDateGroupBox";
        }

        private void deleteToolStripMenuItem_Click(object sender, EventArgs e)
        {

            if (!(resultDataGridView.Rows.Count - 1 > 0) && !(resultDataGridView.SelectedRows.Count > 1))
            {
                string caption = this.resours.GetString("messageBox.Caption");
                MessageBoxButtons buttons = MessageBoxButtons.OK;
                MessageBox.Show(this, "Rows Selection Is Invalid", caption, buttons, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1, MessageBoxOptions.RightAlign);
            }
            else
            {
                DialogResult response;
                response = MessageBox.Show("Are you Sure?", "WARNING", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
                if (response == DialogResult.OK)
                {
                    DataGridViewCell roomName = resultDataGridView.CurrentRow.Cells[0];
                    DataGridViewCell checkIn = resultDataGridView.CurrentRow.Cells[1];
                    DataGridViewCell checkOut = resultDataGridView.CurrentRow.Cells[2];
                    String roomNameSelected = roomName.Value.ToString();
                    String checkInSelected = checkIn.Value.ToString();
                    String checkOutSelected = checkOut.Value.ToString();
                    DateTimeConverter newDateTime = new DateTimeConverter();
                    DateTime checkInDate = (DateTime)newDateTime.ConvertFromString(checkInSelected);
                    DateTime checkOutDate = (DateTime)newDateTime.ConvertFromString(checkOutSelected);
                    int id = RoomStuffManager.GetIdRoomByName(roomNameSelected);
                    Room roomSelected = new Room(id, roomNameSelected);
                    Booking book = new Booking(checkInDate, checkOutDate, roomSelected);
                    try
                    {
                        // DELETE ENTRY!
                        if (BookingManager.DeleteBooking(book))
                        {
                            MessageBox.Show("Succeded!!", "Delete", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            SearchButton_Click(sender, e);
                        }
                        else
                        {
                            MessageBox.Show("Can't Delete the Booking", "Delete", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        }
                    }
                    catch (ArgumentException ex)
                    {
                        string message = ex.Message;
                        string caption = this.resours.GetString("messageBox.Caption");
                        MessageBoxButtons buttons = MessageBoxButtons.OK;
                        MessageBox.Show(this, message, caption, buttons, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1, MessageBoxOptions.RightAlign);
                    }
                }
            }
            
        }

        private void modifyToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (!(resultDataGridView.Rows.Count - 1 > 0) && !(resultDataGridView.SelectedRows.Count > 1))
            {
                string caption = this.resours.GetString("messageBox.Caption");
                MessageBoxButtons buttons = MessageBoxButtons.OK;
                MessageBox.Show(this, "Rows Selection Is Invalid", caption, buttons, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1, MessageBoxOptions.RightAlign);
            }
            else
            {
                MessageBox.Show("Are you Sure?", "WARNING", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                DataGridViewCell roomName = resultDataGridView.CurrentRow.Cells[0];
                DataGridViewCell checkIn = resultDataGridView.CurrentRow.Cells[1];
                DataGridViewCell checkOut = resultDataGridView.CurrentRow.Cells[2];
                String roomNameSelected = roomName.Value.ToString();
                String checkInSelected = checkIn.Value.ToString();
                String checkOutSelected = checkOut.Value.ToString();
                DateTimeConverter newDateTime = new DateTimeConverter();
                DateTime checkInDate = (DateTime)newDateTime.ConvertFromString(checkInSelected);
                DateTime checkOutDate = (DateTime)newDateTime.ConvertFromString(checkOutSelected);
                int id = RoomStuffManager.GetIdRoomByName(roomNameSelected);
                Room roomSelected = new Room(id, roomNameSelected);
                Booking book = new Booking(checkInDate, checkOutDate, roomSelected);
                try
                {
                    // UPDATE ENTRY!
                    /*if (BookingManager.UpdateBooking(book))
                    {
                        MessageBox.Show("Succeded!!", "Delete", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        SearchButton_Click(sender, e);
                    }
                    else
                    {
                        MessageBox.Show("Can't Delete the Booking", "Delete", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }*/
                }
                catch (ArgumentException ex)
                {
                    string message = ex.Message;
                    string caption = this.resours.GetString("messageBox.Caption");
                    MessageBoxButtons buttons = MessageBoxButtons.OK;
                    MessageBox.Show(this, message, caption, buttons, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1, MessageBoxOptions.RightAlign);
                }
            }
        }
    }
}
